<?php
class TwoController extends  IndexController 
{
    public function init()
    {
		parent::init();
        $this->utl              = Yaf_Registry::get("utl");
        $this->CategoryModel    = new CategoryModel();
        $this->WebsiteModel     = new WebsiteModel();
        $this->CategoryTwoModel = new CategoryTwoModel();
        $this->UserLogsModel    = new LogsModel();
        $this->Session          = Yaf_Registry::get("session");
    }

    public function listAction()
    {
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
    	$category_info      = $this->CategoryModel->getCategoryInfo();
        $sites_info         = $this->WebsiteModel->selectSites();
        $category_two_info  = $this->CategoryTwoModel->selectCategoryTwo($startLimit,$offset);
        $allCounts          = $this->CategoryTwoModel->getAllCount();
        $allpage=$this->utl::page('/Two/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $category_two_list  = array();
        foreach ($category_two_info AS $k => $v) 
        {
            $category_two_list[$k] = $v;
            $category_two_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            $category_two_list[$k]['rows'] = $allCounts[0]['rows'];
            $category_two_list[$k]['every_page'] = $offset;
            foreach ($sites_info  AS $ks => $vs) 
            {
                if ($v['sites_id'] === $vs['sites_id']) 
                {
                    $category_two_list[$k]['url'] = $vs['name'];break;
                }
            }
        }
        if(empty($this->Session->user_info))
        {
            $this->redirect('/User/login');
            return true;
        }
        $this->getView()->assign("category_list",$category_info);
        $this->getView()->assign("sites_list",$sites_info);
        $this->getView()->assign("category_two_list",$category_two_list);
        $this->getView()->assign("allpage",$allpage);
    }

    public function pageAction()
    {
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        $category_info      = $this->CategoryModel->getCategoryInfo();
        $sites_info         = $this->WebsiteModel->selectSites();
        $category_two_info  = $this->CategoryTwoModel->selectCategoryTwo($startLimit,$offset);
        $allCounts          = $this->CategoryTwoModel->getAllCount();
        $allpage=$this->utl::page('/Two/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $category_two_list  = array();
        foreach ($category_two_info AS $k => $v) 
        {
            $category_two_list[$k] = $v;
            $category_two_list[$k]['current_time'] = date('Y-m-d H:i:s', (int) $v['create_time']);
            foreach ($sites_info  AS $ks => $vs) 
            {
                if ($v['sites_id'] === $vs['sites_id']) 
                {
                    $category_two_list[$k]['url'] = $vs['name'];break;
                }
            }
        }
        $this->getView()->assign("category_list",$category_info);
        $this->getView()->assign("sites_list",$sites_info);
        $this->getView()->assign("category_two_list",$category_two_list);
        exit(json_encode($category_two_list, true));
    }

    public function currentPageAction()
    {
        $offset             = 10;
        $page               = $this->getRequest()->getParam("page",1);
        $startLimit         = $offset*($page-1);
        $category_two_info  = $this->CategoryTwoModel->selectCategoryTwo($startLimit,$offset);
        $allCounts          = $this->CategoryTwoModel->getAllCount();
        $allpage=$this->utl::page('/Two/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function delAction() 
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) 
        {
            $this->forward('Category', 'article');
            return;
        }
        $rs = $this->CategoryTwoModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "two";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs) 
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        } 
        exit('{"status":true,"info":"删除成功"}');
        return true;                                                                  
    }

    public function editAction() 
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) 
        {
            $this->redirect('Category', 'article');
            return;
        }
        if ($this->getRequest()->isPost()) 
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->CategoryTwoModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "two";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs) 
            {   
                 exit('{"status":false,"info":"编辑失败！"}');
                 return;
            }  
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function addAction() 
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->CategoryTwoModel->add($post);
            $session_add = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "two";
                $session_add[$k]['action'] = "add";
            }
        $this->UserLogsModel->addUser($session_add);
            if (false === $rs) 
            {   
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');
        }
    }

    public function addressAction()
    {
        if ($this->getRequest()->isPost()) {

            header('Content-type:application/json;charset:utf8');
            //$rs = $this->CategoryModel->getNamnById($post);
            $rs = $this->WebsiteModel->selectSites();
            if (false === $rs)
            {
                exit('{"status":false,"info":"一级查找失败！"}');
                return;
            }
            $result  =  json_encode($rs);
            exit($result);
        }
    }

    public function moduleAction()
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            foreach ($post as $key => $value) {

            }
            header('Content-type:application/json;charset:utf8');
            $rs = $this->CategoryModel->getNameById($value);
            $result  =  json_encode($rs);
            if (false === $rs)
            {
                exit('{"status":false,"info":"二级查找失败！"}');
                return;
            }


            exit($result);
        }
    }
}